{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting Thread-1\n",
      "Starting Thread-2\n",
      "Starting Thread-3\n",
      "Thread-3 processing One\n",
      "Thread-1 processing Two\n",
      "Thread-2 processing Three\n",
      "Thread-3 processing Four\n",
      "Thread-1 processing Five\n",
      "Exiting Thread-2\n",
      "Exiting Thread-3\n",
      "Exiting Thread-1\n",
      "Exiting Main Thread\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    " \n",
    "import Queue\n",
    "import threading\n",
    "import time\n",
    " \n",
    "exitFlag = 0\n",
    " \n",
    "class myThread (threading.Thread):\n",
    "    def __init__(self, threadID, name, q):\n",
    "        threading.Thread.__init__(self)\n",
    "        self.threadID = threadID\n",
    "        self.name = name\n",
    "        self.q = q\n",
    "    def run(self):\n",
    "        print \"Starting \" + self.name\n",
    "        process_data(self.name, self.q)\n",
    "        print \"Exiting \" + self.name\n",
    " \n",
    "def process_data(threadName, q):\n",
    "    while not exitFlag:\n",
    "        queueLock.acquire()\n",
    "        if not workQueue.empty():\n",
    "            data = q.get()\n",
    "            queueLock.release()\n",
    "            print \"%s processing %s\" % (threadName, data)\n",
    "        else:\n",
    "            queueLock.release()\n",
    "        time.sleep(1)\n",
    " \n",
    "threadList = [\"Thread-1\", \"Thread-2\", \"Thread-3\"]\n",
    "nameList = [\"One\", \"Two\", \"Three\", \"Four\", \"Five\"]\n",
    "queueLock = threading.Lock()\n",
    "workQueue = Queue.Queue(10)\n",
    "threads = []\n",
    "threadID = 1\n",
    " \n",
    "# 创建新线程\n",
    "for tName in threadList:\n",
    "    thread = myThread(threadID, tName, workQueue)\n",
    "    thread.start()\n",
    "    threads.append(thread)\n",
    "    threadID += 1\n",
    " \n",
    "# 填充队列\n",
    "queueLock.acquire()\n",
    "for word in nameList:\n",
    "    workQueue.put(word)\n",
    "queueLock.release()\n",
    " \n",
    "# 等待队列清空\n",
    "while not workQueue.empty():\n",
    "    pass\n",
    " \n",
    "# 通知线程是时候退出\n",
    "exitFlag = 1\n",
    " \n",
    "# 等待所有线程完成\n",
    "for t in threads:\n",
    "    t.join()\n",
    "print \"Exiting Main Thread\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
